/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.gpsgame.model.test.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
public class DbUtil {

    static {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                GlobalNames.SPRING_CONFIG_FILE_LOCATION);

        transactionManager = (PlatformTransactionManager) context
                .getBean("transactionManager");

    }

    private static PlatformTransactionManager transactionManager;


    public static void populateDb() {
        /*
         * Since this method is supposed to be called form a @BeforeClass
         * method, it works directly with "TransactionManager", since
         * @BeforeClass methods with Spring TestContext do not run in the
         * context of a transaction (which is required for DAOs to work).
         */

        TransactionStatus transactionStatus = transactionManager
                .getTransaction(null);

        try {
        } catch (Throwable e) {
            transactionManager.rollback(transactionStatus);
        }

        transactionManager.commit(transactionStatus);

    }

    public static void cleanDb() throws Exception {
        /*
         * For the same reason as "cleanDb" (with regard to @AfterClass
         * methods), this method works directly with "TransactionManager".
         */

        TransactionStatus transactionStatus = transactionManager
                .getTransaction(null);

        try {

        } catch (Throwable e) {
            transactionManager.rollback(transactionStatus);
        }

        transactionManager.commit(transactionStatus);
    }

}
